package org.wisdom.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.wisdom.vo.errorcode.IErrorCode;
import org.wisdom.vo.errorcode.impl.ErrorCode;

/**
 * 统一返回数据结构体
 * 
 * @author zhangzhiyan
 * @date 2021/06/27 17:57
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

    private Integer code;

    private String msg;

    private T ds;

    public ResponseResult(int code, String message) {
        setCode(code);
        setMsg(message);
    }

    public ResponseResult(int code, String message, T ds) {
        setCode(code);
        setMsg(message);
        setDs(ds);
    }

    public ResponseResult(IErrorCode errorCode, T ds) {
        setCode(errorCode.getCode());
        setMsg(errorCode.getMsg());
        setDs(ds);
    }

    public ResponseResult(IErrorCode errorCode) {
        setCode(errorCode.getCode());
        setMsg(errorCode.getMsg());
    }

    /**
     * 成功的返回对象
     *
     * @author zhangzhiyan
     * @date 2021/06/27 18:44
     * @return org.wisdom.cloud.vo.ResponseResult
     */
    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<>(ErrorCode.SUCCESS);
    }

    /**
     * 成功的返回对象
     * 
     * @author zhangzhiyan
     * @date 2021/06/27 18:44
     * @param data
     *            返回数据对象
     * @return org.wisdom.cloud.vo.ResponseResult
     */
    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(ErrorCode.SUCCESS, data);
    }

    /**
     * 失败的返回对象
     * 
     * @author zhangzhiyan
     * @date 2021/06/27 18:49
     * @param message
     *            异常信息
     * @return org.wisdom.cloud.vo.ResponseResult
     */
    public static <T> ResponseResult<T> fail(String message) {
        return new ResponseResult<>(ErrorCode.BUSINESS_ERROR.getCode(), message);
    }

    /**
     * 失败的返回对象
     * @author 张智研
     * @date 2022/08/29 13:03
     * @param errorCode 错误码
     * @param message 异常信息
     * @return org.wisdom.vo.ResponseResult<T>
     */
    public static <T> ResponseResult<T> fail(int errorCode, String message) {
        return new ResponseResult<>(errorCode, message);
    }

    /**
     * 失败的返回对象
     * @author 张智研
     * @date 2022/08/29 13:03
     * @param errorCode 错误码
     * @return org.wisdom.vo.ResponseResult<T>
     */
    public static <T> ResponseResult<T> fail(ErrorCode errorCode) {
        return new ResponseResult<>(errorCode.getCode(), errorCode.getMsg());
    }
}
